//
//  RNOLTableFormCell.m
//  RongNiuOnline
//
//  Created by apple on 2018/4/25.
//  Copyright © 2018年 rongniu. All rights reserved.
//

#import "RNOLTableFormCell.h"

@implementation RNOLTableFormCell

- (UILabel *)nameLabel{
    if (!_nameLabel) {
        _nameLabel = [RNOLUnitTool createLabelWithText:@"label:" textColor:kTextColor fontSize:15];
        [self.contentView addSubview: _nameLabel];
    }
    return _nameLabel;
}

- (UITextField *)contentTextField{
    if (!_contentTextField) {
        _contentTextField = [[UITextField alloc] init];
        _contentTextField.font = KFontSize(15);
        _contentTextField.keyboardType = UIKeyboardTypeNumberPad;
        _contentTextField.textColor = kTextLightGrayColor;
        [self.contentView addSubview: _contentTextField];
    }
    return _contentTextField;
}

- (UIButton *)rightButton{
    if (!_rightButton) {
        _rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_rightButton setTitle:@"发送验证码" forState:UIControlStateNormal];
        _rightButton.titleLabel.font = KFontSize(10);
        [_rightButton setTitleColor: KWhiteColor forState:UIControlStateNormal];
        _rightButton.backgroundColor = kAppMainColor;
        ViewRadius(_rightButton, 3);
        [_rightButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview: _rightButton];
    }
    return _rightButton;
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = KWhiteColor;
    }
    return self;
}

- (void)setupLayout{
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@15);
        make.width.equalTo(@80);
        make.height.equalTo(@21);
        make.centerY.equalTo(self.contentView.mas_centerY);
    }];
    
     CGFloat rightSpace = self.model.isNeedButton? 95 : 15;
    [self.contentTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLabel.mas_right).offset(5);
        make.right.equalTo(self.contentView.mas_right).offset(-rightSpace);
        make.height.equalTo(@30);
        make.centerY.equalTo(self.contentView.mas_centerY);
    }];
    
    if (self.model.isNeedButton) {
        [self.rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.contentView.mas_right).offset(-15);
            make.height.equalTo(@25);
            make.width.equalTo(@80);
            make.centerY.equalTo(self.contentView.mas_centerY);
        }];
    }
}



- (void)setModel:(RNOLAuthPayModel *)model{
    _model = model;
    [self setupLayout];
    
    self.nameLabel.text = model.name;
    if (model.content.length) {
        self.contentTextField.text = model.content;
    }else{
        self.contentTextField.placeholder = model.placeholder;
    }
    self.rightButton.hidden = !model.isNeedButton;
    self.contentTextField.enabled = model.isEditable;
}


#pragma mark - 获取验证码
- (void)buttonAction:(UIButton *)button{
    [button addStartTime:60 title:@"重新获取" waitTittle:@"秒"];
    
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
